package net.lcqf.http.config;

import cn.hutool.core.util.StrUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.BaseConfig;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Redisson 配置
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2021-2025 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2025/2/10 14:55
 * // +----------------------------------------------------------------------
 */

@Configuration
@ConditionalOnMissingBean(RedissonClient.class)
@Import(RedissonConfig.RedissonClientConfig.class)
public class RedissonConfig {

    private final Config config;
    public RedissonConfig(Config config) {
        this.config = config;
    }

    @Bean
    public RedissonClient redissonClient() { return Redisson.create(config); }


    @ConditionalOnMissingBean(Config.class)
    @EnableConfigurationProperties({RedisProperties.class})
    static class RedissonClientConfig {
        private final RedisProperties redisProperties;

        public RedissonClientConfig(RedisProperties redisProperties) {
            this.redisProperties = redisProperties;
        }

        @Bean
        public Config redissonClientConfig() {
            Config config = new Config();

            SingleServerConfig singleServerConfig = config.useSingleServer();
            String schema = redisProperties.isSsl() ? "rediss://" : "redis://";
            singleServerConfig.setAddress(schema + redisProperties.getHost() + ":" + redisProperties.getPort());
            singleServerConfig.setDatabase(redisProperties.getDatabase());
            baseConfig(singleServerConfig, redisProperties);

            config.setCodec(new JsonJacksonCodec());
            return config;
        }

        /**
         * 其他公共配置
         * @param config config
         * @param properties yml文件中配置参数
         */
        private void baseConfig(BaseConfig config, RedisProperties properties) {
            if (StrUtil.isNotBlank(properties.getPassword())) {
                config.setPassword(properties.getPassword());
            }

            if (properties.getTimeout() != null) {
                config.setTimeout(Long.valueOf(properties.getTimeout().getSeconds() * 1000).intValue());
            }

            if (StrUtil.isNotBlank(properties.getClientName())) {
                config.setClientName(properties.getClientName());
            }
        }
    }
}
